<html>
<head>
<script type="text/javascript" src="newwords.js">
</script>
<script type="text/javascript">
var db=new DictDB();
db.init();
SaveNewWord=function(word,sentence,wordurl){
db.GetWord(word,function(result){
					if(result){
						db.Update(word,sentence,wordurl);
					}else{
						db.NewWord(word,sentence,wordurl);
					}
				}
		);
}

var DefaultOptions =
{
    "color_type":["color_type",0],
    "dict_disable": ["checked", false],
    "ctrl_only": ["checked", false],
    "english_only": ["checked", true],
    "save_chinese":["checked",false],
    "priority":["value","local"]
};
var ColorsChanged = false;

if (localStorage["ColorOptions"] == undefined)
{
    localStorage["ColorOptions"] = JSON.stringify(DefaultOptions);
}

//DRAC - activate plugin by default if apply to all is checked.
var startupOptions = JSON.parse(localStorage["ColorOptions"]);

if(startupOptions["dict_disable"][1]) //如果发现上次被禁用了，那么将它启用
{
    startupOptions["dict_disable"][1] = false;
    localStorage["ColorOptions"] = JSON.stringify(startupOptions);
}

ColorsChanged = true;

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse)
    {
        if (request.init == "init" && ColorsChanged == true)
        {
            sendResponse(
                {
                    init: "globalPages",
                    ChangeColors: "true",
                    ColorOptions: localStorage["ColorOptions"]
                }
            );
        }
        else if(request.init == "new")
        {
		  db.GetWord(request.word,function(res){
              /*
					sendResponse({
						init: "globalPages",
						saved: "ok",
						result:res
					});
                    */
              if(res.info.length>100) //一个有效的html文件至少应该这么长
              {
                  sendResponse({
						init: "globalPages",
						saved: "ok",
						result: res.info
                    });
              }
              else 
              {
                var xhr = new XMLHttpRequest();
                var url='http://dict.cn/mini.php?q=' + request.word;
                console.log("send req:"+url);
                xhr.open("GET",url);
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4) {
                        //console.log("length="+xhr.responseText.length);
                        //console.log(xhr.responseText);
                        if(xhr.responseText.indexOf('class="mut_jies"')==-1) //如果查不到，就不要保存了
                            if(request.save)
                                db.ImportVocabulary(request.word,xhr.responseText);
					sendResponse({
						init: "globalPages",
						saved: "ok",
						result: xhr.responseText
                    });
                }
                }
                xhr.send();
              }
				}
		  );
        }
		else if (request.init=="saveword" ) 
		{
		//如果不存在，那么就创建否则更新。
			db.NewWord(request.word,request.sentence,request.wordurl);
			sendResponse({
						init: "globalPages",
						saved: "ok"
                    });
		}
    }
);
</script>
</head>
<body>
</body>
</html>
